package com.qf.servlet;

import com.qf.entity.StuInfo;
import com.qf.service.Impl.StulnfoServiceImpl;
import com.qf.service.StulnfoService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author a1275872671
 * @version v1.0
 * @project guanqipeng
 * @package com.qf.servlet
 * @company
 * @date 2024/4/30 12:47
 */
@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        String stuName = request.getParameter("stuName");
        String shuno = request.getParameter("shuno");
        String pwd = request.getParameter("pwd");
        String idcard = request.getParameter("idcard");
        String startDate = request.getParameter("startDate");
        if (id==null||id.trim().equals("")){
            response.getWriter().write("不能为空");
        }
        if (stuName==null||stuName.trim().equals("")){
            response.getWriter().write("不能为空");
        }
        if (shuno==null||shuno.trim().equals("")){
            response.getWriter().write("不能为空");
        }
        if (pwd==null||pwd.trim().equals("")){
            response.getWriter().write("不能为空");
        }
        if (idcard==null||idcard.trim().equals("")){
            response.getWriter().write("不能为空");
        }
        if (startDate==null||startDate.trim().equals("")){
            response.getWriter().write("不能为空");
        }
        StulnfoService addStuService=new StulnfoServiceImpl();
        try {
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate localDate = LocalDate.parse(startDate, dtf);
            LocalDateTime localDateTime = localDate.atStartOfDay();
            StuInfo stuInfo=new StuInfo(Integer.parseInt(id),stuName,shuno,pwd,idcard,localDateTime,0);
            addStuService.updateStu(stuInfo);
            response.sendRedirect("stulnfo.jsp");
        } catch(Exception e) {
            e.printStackTrace();
            response.sendRedirect("updata.jsp");
        }

    }
}
